package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.collection.CollUtil;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.pojo.TerAlarm;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.business.TerAlarmService;
import com.tbit.uqbike.service.rpcservice.PurviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 历史告警
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "/terAlarm", produces = BaseController.MEDIA_JSON_UTF8)
public class TerAlarmController extends BaseController {
    @Autowired
    private TerAlarmService terAlarmService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private PurviewService purviewService;


    /* 查询历史告警 */
    @RequestMapping("/query")
    public JsonResult query(String userCode,
                            @NotNull(message = "ver.startTime.notNull") Long startTime,
                            @NotNull(message = "ver.endTime.notNull") Long endTime,
                            @RequestParam(defaultValue = "10") Integer rowCount,
                            @RequestParam(defaultValue = "1") Integer pageNO,
                            Integer alarmType, HttpServletRequest request) {

        /*查询设备信息*/
        LoginInfo loginInfo = getLogin(request);
        Machine machine = machineService.getByUserCode(userCode);
        if (machine == null) {
            throw new BaseException(SystemCodeEnum.USERCODE_NOT_EXIST);
        }

        //权限
        purviewService.purviewCheck(loginInfo.id, machine.getAccountId());

        List<TerAlarm> history = terAlarmService.getByMachineNOAndTime(machine.getMachineNO(), alarmType, startTime, endTime, rowCount, pageNO);
        Integer total = terAlarmService.getTotal(machine.getMachineNO(), alarmType, startTime, endTime);

        if (CollUtil.isNotEmpty(history)) {
            for (TerAlarm ta : history) {
                ta.setUserCode(userCode);
            }
        }
        return JsonResult.ok(history, total);
    }

    /**
     * 导出
     * @return 文件名
     */
    @GetMapping("/export")
    public Result<Object> exportExcel(String userCode,
                                      @NotNull(message = "ver.startTime.notNull") Long startTime,
                                      @NotNull(message = "ver.endTime.notNull") Long endTime,
                                      Integer alarmType, HttpServletRequest request){
        /*查询设备信息*/
        LoginInfo loginInfo = getLogin(request);
        Machine machine = machineService.getByUserCode(userCode);
        if (machine == null) {
            throw new BaseException(SystemCodeEnum.USERCODE_NOT_EXIST);
        }

        //权限
        purviewService.purviewCheck(loginInfo.id, machine.getAccountId());
        return terAlarmService.exportExcel(machine.getMachineNO(), alarmType, startTime, endTime, userCode);
    }

}
